<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>列表的增删改查 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter03.html" />
    
    
    <link rel="prev" href="./chapter01.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="2"
        data-chapter-title="列表的增删改查"
        data-filepath="chapter02.md"
        data-basepath="."
        data-revision="Fri Feb 07 2020 10:06:55 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        列表的基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        列表的增删改查
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        列表的遍历
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        列表的嵌套
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        列表推导式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        列表的复制
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x5217;&#x8868;&#x7684;&#x6570;&#x636E;&#x64CD;&#x4F5C;">&#x5217;&#x8868;&#x7684;&#x6570;&#x636E;&#x64CD;&#x4F5C;</h2>
<p>&#x6211;&#x4EEC;&#x5BF9;&#x4E8E;&#x53EF;&#x53D8;&#x6570;&#x636E;&#xFF08;&#x4F8B;&#x5982;&#xFF0C;&#x5217;&#x8868;&#xFF0C;&#x6570;&#x636E;&#x5E93;&#x7B49;&#xFF09;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x4E00;&#x822C;&#x5305;&#x542B;&#x589E;&#x3001;&#x5220;&#x3001;&#x6539;&#x3001;&#x67E5;&#x56DB;&#x4E2A;&#x65B9;&#x9762;&#x3002;</p>
<h3 id="&#x4E00;&#x3001;&#x6DFB;&#x52A0;&#x5143;&#x7D20;">&#x4E00;&#x3001;&#x6DFB;&#x52A0;&#x5143;&#x7D20;</h3>
<p>&#x6DFB;&#x52A0;&#x5143;&#x7D20;&#x6709;&#x4E00;&#x4E0B;&#x51E0;&#x4E2A;&#x65B9;&#x6CD5;:</p>
<ul>
<li>append &#x5728;&#x672B;&#x5C3E;&#x6DFB;&#x52A0;&#x5143;&#x7D20;</li>
<li>insert &#x5728;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x63D2;&#x5165;&#x5143;&#x7D20;</li>
<li>extend &#x5408;&#x5E76;&#x4E24;&#x4E2A;&#x5217;&#x8868;</li>
</ul>
<h4 id="append">append</h4>
<p>append&#x4F1A;&#x628A;&#x65B0;&#x5143;&#x7D20;&#x6DFB;&#x52A0;&#x5230;&#x5217;&#x8868;&#x672B;&#x5C3E;</p>
<pre><code class="lang-python">    <span class="hljs-comment">#&#x5B9A;&#x4E49;&#x53D8;&#x91CF;A&#xFF0C;&#x9ED8;&#x8BA4;&#x6709;3&#x4E2A;&#x5143;&#x7D20;</span>
    A = [<span class="hljs-string">&apos;xiaoWang&apos;</span>,<span class="hljs-string">&apos;xiaoZhang&apos;</span>,<span class="hljs-string">&apos;xiaoHua&apos;</span>]

    print(<span class="hljs-string">&quot;-----&#x6DFB;&#x52A0;&#x4E4B;&#x524D;&#xFF0C;&#x5217;&#x8868;A&#x7684;&#x6570;&#x636E;-----A=%s&quot;</span> % A)

    <span class="hljs-comment">#&#x63D0;&#x793A;&#x3001;&#x5E76;&#x6DFB;&#x52A0;&#x5143;&#x7D20;</span>
    temp = input(<span class="hljs-string">&apos;&#x8BF7;&#x8F93;&#x5165;&#x8981;&#x6DFB;&#x52A0;&#x7684;&#x5B66;&#x751F;&#x59D3;&#x540D;:&apos;</span>)
    A.append(temp)

    print(<span class="hljs-string">&quot;-----&#x6DFB;&#x52A0;&#x4E4B;&#x540E;&#xFF0C;&#x5217;&#x8868;A&#x7684;&#x6570;&#x636E;-----A=%s&quot;</span> % A)
</code></pre>
<h4 id="insert">insert</h4>
<p>insert(index, object) &#x5728;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;index&#x524D;&#x63D2;&#x5165;&#x5143;&#x7D20;object</p>
<pre><code class="lang-python">strs = [<span class="hljs-string">&apos;a&apos;</span>,<span class="hljs-string">&apos;b&apos;</span>,<span class="hljs-string">&apos;m&apos;</span>,<span class="hljs-string">&apos;s&apos;</span>]
strs.insert(<span class="hljs-number">3</span>,<span class="hljs-string">&apos;h&apos;</span>)
print(strs)  <span class="hljs-comment"># [&apos;a&apos;, &apos;b&apos;, &apos;m&apos;, &apos;h&apos;, &apos;s&apos;]</span>
</code></pre>
<h4 id="extend">extend</h4>
<p>&#x901A;&#x8FC7;extend&#x53EF;&#x4EE5;&#x5C06;&#x53E6;&#x4E00;&#x4E2A;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#x9010;&#x4E00;&#x6DFB;&#x52A0;&#x5230;&#x5217;&#x8868;&#x4E2D;</p>
<pre><code class="lang-python">a = [<span class="hljs-string">&apos;a&apos;</span>,<span class="hljs-string">&apos;b&apos;</span>,<span class="hljs-string">&apos;c&apos;</span>]
b = [<span class="hljs-string">&apos;d&apos;</span>,<span class="hljs-string">&apos;e&apos;</span>,<span class="hljs-string">&apos;f&apos;</span>]
a.extend(b)
print(a)  <span class="hljs-comment"># [&apos;a&apos;, &apos;b&apos;, &apos;c&apos;, &apos;d&apos;, &apos;e&apos;, &apos;f&apos;] &#x5C06; b &#x6DFB;&#x52A0;&#x5230; a &#x91CC;</span>
print(b) <span class="hljs-comment"># [&apos;d&apos;,&apos;e&apos;,&apos;f&apos;] b&#x7684;&#x5185;&#x5BB9;&#x4E0D;&#x53D8;</span>
</code></pre>
<h3 id="&#x4E8C;&#x3001;&#x4FEE;&#x6539;&#x5143;&#x7D20;">&#x4E8C;&#x3001;&#x4FEE;&#x6539;&#x5143;&#x7D20;</h3>
<p>&#x6211;&#x4EEC;&#x662F;&#x901A;&#x8FC7;&#x6307;&#x5B9A;&#x4E0B;&#x6807;&#x6765;&#x8BBF;&#x95EE;&#x5217;&#x8868;&#x5143;&#x7D20;&#xFF0C;&#x56E0;&#x6B64;&#x4FEE;&#x6539;&#x5143;&#x7D20;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4E3A;&#x6307;&#x5B9A;&#x7684;&#x5217;&#x8868;&#x4E0B;&#x6807;&#x8D4B;&#x503C;&#x5373;&#x53EF;&#x3002;</p>
<pre><code class="lang-python">    <span class="hljs-comment">#&#x5B9A;&#x4E49;&#x53D8;&#x91CF;A&#xFF0C;&#x9ED8;&#x8BA4;&#x6709;3&#x4E2A;&#x5143;&#x7D20;</span>
    A = [<span class="hljs-string">&apos;xiaoWang&apos;</span>,<span class="hljs-string">&apos;xiaoZhang&apos;</span>,<span class="hljs-string">&apos;xiaoHua&apos;</span>]

    print(<span class="hljs-string">&quot;-----&#x4FEE;&#x6539;&#x4E4B;&#x524D;&#xFF0C;&#x5217;&#x8868;A&#x7684;&#x6570;&#x636E;-----A=%s&quot;</span> % A)

    <span class="hljs-comment">#&#x4FEE;&#x6539;&#x5143;&#x7D20;</span>
    A[<span class="hljs-number">1</span>] = <span class="hljs-string">&apos;xiaoLu&apos;</span>

    print(<span class="hljs-string">&quot;-----&#x4FEE;&#x6539;&#x4E4B;&#x540E;&#xFF0C;&#x5217;&#x8868;A&#x7684;&#x6570;&#x636E;-----A=%s&quot;</span> % A)
</code></pre>
<h2 id="&#x4E09;&#x3001;&#x67E5;&#x627E;&#x5143;&#x7D20;">&#x4E09;&#x3001;&#x67E5;&#x627E;&#x5143;&#x7D20;</h2>
<p>&#x6240;&#x8C13;&#x7684;&#x67E5;&#x627E;&#xFF0C;&#x5C31;&#x662F;&#x770B;&#x770B;&#x6307;&#x5B9A;&#x7684;&#x5143;&#x7D20;&#x662F;&#x5426;&#x5B58;&#x5728;&#xFF0C;&#x4EE5;&#x53CA;&#x67E5;&#x770B;&#x5143;&#x7D20;&#x6240;&#x5728;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x4E3B;&#x8981;&#x5305;&#x542B;&#x4E00;&#x4E0B;&#x51E0;&#x4E2A;&#x65B9;&#x6CD5;&#xFF1A;</p>
<ul>
<li>in &#x548C; not in</li>
<li>index &#x548C; count</li>
</ul>
<h4 id="in-not-in">in, not in</h4>
<p>python&#x4E2D;&#x67E5;&#x627E;&#x7684;&#x5E38;&#x7528;&#x65B9;&#x6CD5;&#x4E3A;&#xFF1A;</p>
<ul>
<li>in&#xFF08;&#x5B58;&#x5728;&#xFF09;,&#x5982;&#x679C;&#x5B58;&#x5728;&#x90A3;&#x4E48;&#x7ED3;&#x679C;&#x4E3A;true&#xFF0C;&#x5426;&#x5219;&#x4E3A;false</li>
<li>not in&#xFF08;&#x4E0D;&#x5B58;&#x5728;&#xFF09;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#x90A3;&#x4E48;&#x7ED3;&#x679C;&#x4E3A;true&#xFF0C;&#x5426;&#x5219;false</li>
</ul>
<pre><code class="lang-python">    <span class="hljs-comment">#&#x5F85;&#x67E5;&#x627E;&#x7684;&#x5217;&#x8868;</span>
    nameList = [<span class="hljs-string">&apos;xiaoWang&apos;</span>,<span class="hljs-string">&apos;xiaoZhang&apos;</span>,<span class="hljs-string">&apos;xiaoHua&apos;</span>]

    <span class="hljs-comment">#&#x83B7;&#x53D6;&#x7528;&#x6237;&#x8981;&#x67E5;&#x627E;&#x7684;&#x540D;&#x5B57;</span>
    findName = input(<span class="hljs-string">&apos;&#x8BF7;&#x8F93;&#x5165;&#x8981;&#x67E5;&#x627E;&#x7684;&#x59D3;&#x540D;:&apos;</span>)

    <span class="hljs-comment">#&#x67E5;&#x627E;&#x662F;&#x5426;&#x5B58;&#x5728;</span>
    <span class="hljs-keyword">if</span> findName <span class="hljs-keyword">in</span> nameList:
        print(<span class="hljs-string">&apos;&#x5728;&#x5217;&#x8868;&#x4E2D;&#x627E;&#x5230;&#x4E86;&#x76F8;&#x540C;&#x7684;&#x540D;&#x5B57;&apos;</span>)
    <span class="hljs-keyword">else</span>:
        print(<span class="hljs-string">&apos;&#x6CA1;&#x6709;&#x627E;&#x5230;&apos;</span>)
</code></pre>
<p>&#x7ED3;&#x679C;1&#xFF1A;(&#x627E;&#x5230;)</p>
<p><img src="imgs/find.gif" alt=""></p>
<p>&#x7ED3;&#x679C;2&#xFF1A;(&#x6CA1;&#x6709;&#x627E;&#x5230;)</p>
<p><img src="imgs/notfound.gif" alt=""></p>
<p>&#x8BF4;&#x660E;&#xFF1A;</p>
<blockquote>
<p>in&#x7684;&#x65B9;&#x6CD5;&#x53EA;&#x8981;&#x4F1A;&#x7528;&#x4E86;&#xFF0C;&#x90A3;&#x4E48;not in&#x4E5F;&#x662F;&#x540C;&#x6837;&#x7684;&#x7528;&#x6CD5;&#xFF0C;&#x53EA;&#x4E0D;&#x8FC7;not in&#x5224;&#x65AD;&#x7684;&#x662F;&#x4E0D;&#x5B58;&#x5728;</p>
</blockquote>
<h4 id="index-count">index, count</h4>
<p>index&#x7528;&#x6765;&#x67E5;&#x627E;&#x5143;&#x7D20;&#x6240;&#x5728;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x5982;&#x679C;&#x672A;&#x627E;&#x5230;&#x5219;&#x4F1A;&#x62A5;&#x9519;&#xFF1B;count&#x7528;&#x6765;&#x8BA1;&#x7B97;&#x67D0;&#x4E2A;&#x5143;&#x7D20;&#x51FA;&#x73B0;&#x7684;&#x6B21;&#x6570;&#x3002;&#x5B83;&#x4EEC;&#x7684;&#x4F7F;&#x7528;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x91CC;&#x7684;&#x4F7F;&#x7528;&#x6548;&#x679C;&#x4E00;&#x81F4;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span>a = [<span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-string">&apos;b&apos;</span>, <span class="hljs-string">&apos;c&apos;</span>, <span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-string">&apos;b&apos;</span>]
<span class="hljs-prompt">&gt;&gt;&gt; </span>a.index(<span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>) <span class="hljs-comment"># &#x6CE8;&#x610F;&#x662F;&#x5DE6;&#x95ED;&#x53F3;&#x5F00;&#x533A;&#x95F4;</span>
Traceback (most recent call last):
  File <span class="hljs-string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
ValueError: <span class="hljs-string">&apos;a&apos;</span> <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> list
<span class="hljs-prompt">&gt;&gt;&gt; </span>a.index(<span class="hljs-string">&apos;a&apos;</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>)
<span class="hljs-number">3</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>a.count(<span class="hljs-string">&apos;b&apos;</span>)
<span class="hljs-number">2</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>a.count(<span class="hljs-string">&apos;d&apos;</span>)
<span class="hljs-number">0</span>
</code></pre>
<h3 id="&#x56DB;&#x3001;&#x5220;&#x9664;&#x5143;&#x7D20;">&#x56DB;&#x3001;&#x5220;&#x9664;&#x5143;&#x7D20;</h3>
<p>&#x7C7B;&#x6BD4;&#x73B0;&#x5B9E;&#x751F;&#x6D3B;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x67D0;&#x4F4D;&#x540C;&#x5B66;&#x8C03;&#x73ED;&#x4E86;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x5E94;&#x8BE5;&#x628A;&#x8FD9;&#x4E2A;&#x6761;&#x8D70;&#x540E;&#x7684;&#x5B66;&#x751F;&#x7684;&#x59D3;&#x540D;&#x5220;&#x9664;&#x6389;&#xFF1B;&#x5728;&#x5F00;&#x53D1;&#x4E2D;&#x7ECF;&#x5E38;&#x4F1A;&#x7528;&#x5230;&#x5220;&#x9664;&#x8FD9;&#x79CD;&#x529F;&#x80FD;&#x3002;</p>
<p>&#x5217;&#x8868;&#x5143;&#x7D20;&#x7684;&#x5E38;&#x7528;&#x5220;&#x9664;&#x65B9;&#x6CD5;&#x6709;&#xFF1A;</p>
<ul>
<li>del&#xFF1A;&#x6839;&#x636E;&#x4E0B;&#x6807;&#x8FDB;&#x884C;&#x5220;&#x9664;</li>
<li>pop&#xFF1A;&#x5220;&#x9664;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5143;&#x7D20;</li>
<li>remove&#xFF1A;&#x6839;&#x636E;&#x5143;&#x7D20;&#x7684;&#x503C;&#x8FDB;&#x884C;&#x5220;&#x9664;</li>
</ul>
<h3 id="del">del</h3>
<pre><code class="lang-python">movieName = [<span class="hljs-string">&apos;&#x52A0;&#x52D2;&#x6BD4;&#x6D77;&#x76D7;&apos;</span>,<span class="hljs-string">&apos;&#x9A87;&#x5BA2;&#x5E1D;&#x56FD;&apos;</span>,<span class="hljs-string">&apos;&#x7B2C;&#x4E00;&#x6EF4;&#x8840;&apos;</span>,<span class="hljs-string">&apos;&#x6307;&#x73AF;&#x738B;&apos;</span>,<span class="hljs-string">&apos;&#x970D;&#x6BD4;&#x7279;&#x4EBA;&apos;</span>,<span class="hljs-string">&apos;&#x901F;&#x5EA6;&#x4E0E;&#x6FC0;&#x60C5;&apos;</span>]
print(<span class="hljs-string">&apos;------&#x5220;&#x9664;&#x4E4B;&#x524D;------movieName=%s&apos;</span> % movieName)
<span class="hljs-keyword">del</span> movieName[<span class="hljs-number">2</span>]
print(<span class="hljs-string">&apos;------&#x5220;&#x9664;&#x4E4B;&#x540E;------movieName=%s&apos;</span> % movieName)
</code></pre>
<h3 id="pop">pop</h3>
<pre><code class="lang-python">movieName = [<span class="hljs-string">&apos;&#x52A0;&#x52D2;&#x6BD4;&#x6D77;&#x76D7;&apos;</span>,<span class="hljs-string">&apos;&#x9A87;&#x5BA2;&#x5E1D;&#x56FD;&apos;</span>,<span class="hljs-string">&apos;&#x7B2C;&#x4E00;&#x6EF4;&#x8840;&apos;</span>,<span class="hljs-string">&apos;&#x6307;&#x73AF;&#x738B;&apos;</span>,<span class="hljs-string">&apos;&#x970D;&#x6BD4;&#x7279;&#x4EBA;&apos;</span>,<span class="hljs-string">&apos;&#x901F;&#x5EA6;&#x4E0E;&#x6FC0;&#x60C5;&apos;</span>]
print(<span class="hljs-string">&apos;------&#x5220;&#x9664;&#x4E4B;&#x524D;------movieName=%s&apos;</span> % movieName)
movieName.pop()
print(<span class="hljs-string">&apos;------&#x5220;&#x9664;&#x4E4B;&#x540E;------movieName=%s&apos;</span> % movieName)
</code></pre>
<h3 id="remove">remove</h3>
<pre><code class="lang-python">movieName = [<span class="hljs-string">&apos;&#x52A0;&#x52D2;&#x6BD4;&#x6D77;&#x76D7;&apos;</span>,<span class="hljs-string">&apos;&#x9A87;&#x5BA2;&#x5E1D;&#x56FD;&apos;</span>,<span class="hljs-string">&apos;&#x7B2C;&#x4E00;&#x6EF4;&#x8840;&apos;</span>,<span class="hljs-string">&apos;&#x6307;&#x73AF;&#x738B;&apos;</span>,<span class="hljs-string">&apos;&#x970D;&#x6BD4;&#x7279;&#x4EBA;&apos;</span>,<span class="hljs-string">&apos;&#x901F;&#x5EA6;&#x4E0E;&#x6FC0;&#x60C5;&apos;</span>]
print(<span class="hljs-string">&apos;------&#x5220;&#x9664;&#x4E4B;&#x524D;------movieName=%s&apos;</span> % movieName)
movieName.remove(<span class="hljs-string">&apos;&#x6307;&#x73AF;&#x738B;&apos;</span>)
print(<span class="hljs-string">&apos;------&#x5220;&#x9664;&#x4E4B;&#x540E;------movieName=%s&apos;</span> % movieName)
</code></pre>
<h3 id="&#x4E94;&#x3001;&#x6392;&#x5E8F;sort-reverse">&#x4E94;&#x3001;&#x6392;&#x5E8F;(sort, reverse)</h3>
<p>sort&#x65B9;&#x6CD5;&#x662F;&#x5C06;list&#x6309;&#x7279;&#x5B9A;&#x987A;&#x5E8F;&#x91CD;&#x65B0;&#x6392;&#x5217;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;&#x7531;&#x5C0F;&#x5230;&#x5927;&#xFF0C;&#x53C2;&#x6570;reverse=True&#x53EF;&#x6539;&#x4E3A;&#x5012;&#x5E8F;&#xFF0C;&#x7531;&#x5927;&#x5230;&#x5C0F;&#x3002;</p>
<p>reverse&#x65B9;&#x6CD5;&#x662F;&#x5C06;list&#x9006;&#x7F6E;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span>a = [<span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
<span class="hljs-prompt">&gt;&gt;&gt; </span>a
[<span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
<span class="hljs-prompt">&gt;&gt;&gt; </span>a.reverse()  <span class="hljs-comment"># &#x9006;&#x7F6E;&#xFF0C;&#x4E0D;&#x6392;&#x5E8F;</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>a
[<span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">1</span>]
<span class="hljs-prompt">&gt;&gt;&gt; </span>a.sort()  <span class="hljs-comment"># &#x9ED8;&#x8BA4;&#x4ECE;&#x5C0F;&#x5230;&#x5927;&#x6392;&#x5E8F;</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>a
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]
<span class="hljs-prompt">&gt;&gt;&gt; </span>a.sort(reverse=<span class="hljs-keyword">True</span>)  <span class="hljs-comment"># &#x4ECE;&#x5927;&#x5230;&#x5C0F;&#x6392;&#x5E8F;</span>
<span class="hljs-prompt">&gt;&gt;&gt; </span>a
[<span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]
</code></pre>
<h3 id="&#x7EC3;&#x4E60;">&#x7EC3;&#x4E60;</h3>
<p>&#x8BF7;&#x5220;&#x9664;&#x5217;&#x8868; <code>words = [&apos;hello&apos;,&apos;&apos;,&apos;,&apos;good&apos;,&apos;hi&apos;,&apos;&apos;,&apos;yes&apos;,&apos;&apos;,&apos;no&apos;]</code> &#x91CC;&#x6240;&#x6709;&#x7684;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter01.html" class="navigation navigation-prev " aria-label="Previous page: 列表的基本使用"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter03.html" class="navigation navigation-next " aria-label="Next page: 列表的遍历"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
